package com.xzmzhp.dao.maintain.sql;

import com.xzmzhp.utils.DateUtil;
import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 16:31
 * @program: ams
 * @description: 破损类型相关sql
 */
public class BreakageSql {

    /**
     * 根据参数查询数据sql
     * @param param 参数
     *        itemId 物品id
     *        itemName 物品名称
     *        stdmodeId  物品类型id
     *        breakageType 破损状态
     *        startTime 开始时间
     *        endTime  结束时间
     * @return 查询数据sql
     */
    public String findBreakageByParam(Map<String,Object> param){
        //物品id
        Integer itemId = Integer.parseInt(param.get("itemId").toString());
        //物品名称
        String itemName = param.get("itemName").toString();
        //物品类型id
        Integer stdmodeId = Integer.parseInt(param.get("stdmodeId").toString());
        //破损状态
        Integer breakageType = Integer.parseInt(param.get("breakageType").toString());
        //破损时间
        Date startTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("startTime").toString());
        Date endTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("endTime").toString());
        return new SQL(){
            {
                SELECT("breakage_id,breakage_part,breakage_url,breakage_date,breakage_type,item_id,item_name,stdmode_id,stdmode_name");
                FROM("breakage");
                if (StringUtils.isNotEmpty(itemName)) {
                    WHERE("item_name = '%" + itemName + "%'");
                }
                if (StringUtils.isNotNull(itemId)) {
                    WHERE("item_id = " + itemId);
                }
                if (StringUtils.isNotNull(stdmodeId)) {
                    WHERE("stdmode_id = " + stdmodeId);
                }
                if (StringUtils.isNotNull(breakageType)) {
                    WHERE("breakage_type = " + breakageType);
                }
                if (StringUtils.isNotNull(startTime)) {
                    WHERE("breakage_date >= " + startTime);
                }
                if (StringUtils.isNotNull(endTime)) {
                    WHERE("breakage_date <= " + endTime);
                }
                ORDER_BY("breakage_type");
            }
        }.toString();
    }
}
